<?php

/**
 * This is the model class for table "customer".
 *
 * The followings are the available columns in table 'customer':
 * @property integer $customer_id
 * @property integer $store_id
 * @property string $firstname
 * @property string $lastname
 * @property string $email
 * @property string $telephone
 * @property string $fax
 * @property string $password
 * @property string $salt
 * @property string $cart
 * @property string $wishlist
 * @property integer $newsletter
 * @property integer $address_id
 * @property integer $customer_group_id
 * @property string $ip
 * @property integer $status
 * @property integer $approved
 * @property string $token
 * @property string $date_added
 * @property string $pwd
 */
class Customer extends CActiveRecord
{
    public $retypePassword;
    public $verifyCode;

	public function tableName()
	{
		return 'customer';
	}

	public function rules()
	{
		return array(
			array('store_id, newsletter, address_id, customer_group_id', 'numerical', 'integerOnly'=>true, 'on'=>'insert,update'),
			array('firstname, lastname, telephone, fax', 'length', 'max'=>32, 'on'=>'insert,update,register'),
            array('firstname', 'required', 'message'=>Yii::t('account/customer','errorFirstName'), 'on'=>'register'),
			array('email', 'email', 'message'=>Yii::t('account/customer','errorEmail'), 'on'=>'login,insert,update,register'),
            array('email', 'unique', 'message'=>Yii::t('account/customer','errorEmailUnique'), 'on'=>'insert,update,register'),
            array('password', 'required', 'message'=>Yii::t('account/customer','errorPasswordMin'), 'on'=>'login,insert,update,register'),
			array('password, ip, pwd', 'length', 'max'=>40,  'message'=>Yii::t('account/customer','errorPasswordMax'), 'on'=>'login,insert,update,register'),
            array('password', 'length', 'min'=>4, 'message'=>Yii::t('account/customer','errorPasswordMin'), 'on'=>'login,insert,update,register'),
            array('retypePassword','compare','compareAttribute'=>'password','message'=>Yii::t('account/customer','errorRetypePassword'),'on'=>'register'),
			array('salt', 'length', 'max'=>40, 'on'=>'insert,update'),
			array('token', 'length', 'max'=>255, 'on'=>'insert,update'),
			array('cart, wishlist, date_added', 'safe', 'on'=>'insert,update'),
            array('verifyCode', 'captcha', 'allowEmpty'=>!Yii::app()->user->isGuest || !CCaptcha::checkRequirements(),
                'message'=>Yii::t('account/customer','errorСaptcha'),'on'=>'register'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('customer_id, store_id, firstname, lastname, email, telephone, fax, password, salt, cart, wishlist, newsletter, address_id, customer_group_id, ip, status, approved, token, date_added, pwd', 'safe', 'on'=>'search'),
		);
	}

	public function relations()
	{
		return array(
		);
	}

	public function attributeLabels()
	{
		return array(
			'customer_id' => 'Customer',
			'store_id' => 'Store',
			'firstname' => 'Firstname',
			'lastname' => 'Lastname',
			'email' => 'Email',
			'telephone' => 'Telephone',
			'fax' => 'Fax',
			'password' => 'Password',
            'retypePassword'=>'retypePassword',
			'salt' => 'Salt',
			'cart' => 'Cart',
			'wishlist' => 'Wishlist',
			'newsletter' => 'Newsletter',
			'address_id' => 'Address',
			'customer_group_id' => 'Customer Group',
			'ip' => 'Ip',
			'status' => 'Status',
			'approved' => 'Approved',
			'token' => 'Token',
			'date_added' => 'Date Added',
			'pwd' => 'Pwd',
            'verifyCode' => 'Код проверки',
		);
	}

    public function getId()
    {
        return $this->customer_id;
    }

	public function search()
	{
		$criteria=new CDbCriteria;

		$criteria->compare('customer_id',$this->customer_id);
		$criteria->compare('store_id',$this->store_id);
		$criteria->compare('firstname',$this->firstname,true);
		$criteria->compare('lastname',$this->lastname,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('telephone',$this->telephone,true);
		$criteria->compare('fax',$this->fax,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('salt',$this->salt,true);
		$criteria->compare('cart',$this->cart,true);
		$criteria->compare('wishlist',$this->wishlist,true);
		$criteria->compare('newsletter',$this->newsletter);
		$criteria->compare('address_id',$this->address_id);
		$criteria->compare('customer_group_id',$this->customer_group_id);
		$criteria->compare('ip',$this->ip,true);
		$criteria->compare('status',$this->status);
		$criteria->compare('approved',$this->approved);
		$criteria->compare('token',$this->token,true);
		$criteria->compare('date_added',$this->date_added,true);
		$criteria->compare('pwd',$this->pwd,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

    public function beforeSave()
    {
        if($this->isNewRecord){
            $this->pwd=$this->password;
            $this->password=Yii::app()->salt->getHash($this->password.$this->email);
            $this->salt=Yii::app()->salt->rand;
        }
        return parent::beforeSave();
    }
}
